package de.jmda.gen.java.impl;

import static org.junit.Assert.assertTrue;

import org.apache.log4j.Logger;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;
import de.jmda.gen.java.ClassGenerator;
import de.jmda.gen.java.InstanceFieldGenerator;
import de.jmda.gen.java.InstanceMethodGenerator;
import de.jmda.gen.java.TypeKindGenerator;

public class JUTDefaultClassGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(JUTDefaultClassGenerator.class);

	@Test
	public void testDefaultClassGenerator()
			throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");
		
		String expected =
				TypeKindGenerator.CLASS.getTypeKind() + " " +
				DefaultDeclaredTypeGenerator.DEFAULT_TYPE_NAME +
				System.lineSeparator() +
				DefaultTypeBodyGenerator.DEFAULT_TYPE_BODY;
		
		LOGGER.debug("expected [" + expected + "]");

		assertTrue(
				"unexpected declared element [" + generated + "]",
				expected.contentEquals(generated));
	}

	@Test
	public void testDefaultClassGeneratorWithInstanceMethod()
			throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();

		InstanceMethodGenerator instanceMethodDeclarationGenerator =
				new DefaultInstanceMethodGenerator();

		instanceMethodDeclarationGenerator.setLineIndenter(LineIndenter.TAB_SINGLE);

		generator.addMethodGenerator(instanceMethodDeclarationGenerator);

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");
		
		assertTrue(
				"can't find method",
				generated.indexOf(DefaultMethodNameGenerator.DEFAULT_METHOD_NAME) > 0);
	}

	@Test
	public void testDefaultClassGeneratorWithInstanceField()
			throws GeneratorException
	{
		ClassGenerator generator = new DefaultClassGenerator();

		InstanceFieldGenerator instanceFieldDeclarationGenerator =
				new DefaultInstanceFieldGenerator();

		instanceFieldDeclarationGenerator.setLineIndenter(LineIndenter.TAB_SINGLE);

		generator.addFieldGenerator(instanceFieldDeclarationGenerator);

		StringBuffer generated = generator.generate();

		LOGGER.debug("generated [" + generated + "]");
		
		assertTrue("can't find field", generated.indexOf("field_name") > 0);
	}
}